package com.lbb.springboot.util;


import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CheckUtil {

	public static boolean isMobileNO(String mobiles) {
		Pattern p = Pattern
				.compile("^((13[0-9])|(15[^4,\\D])|(17[0-9])|(18[0-9]))\\d{8}$");
		Matcher m = p.matcher(mobiles);
		System.out.println(m.matches() + "---");
		return m.matches();
	}

	/**
	 * 判断两个string是否相等
	 */
	public static boolean checkEquels(Object strObj0, Object strObj1) {
		String str0 = strObj0 + "";
		String str1 = strObj1 + "";
		if (str0.equals(str1)) {
			return true;
		}
		return false;
	}

	/**
	 * 同时�?��多个参数是否为空
	 * 
	 * @param strArray
	 * @return
	 */
	public static boolean isNull(Object... strArray) {
		for (Object obj : strArray) {
			if (!"".equals(obj + "")) {
				return false;
			}
		}
		return true;
	}

	/**
	 * �?��对象是否为空
	 * 
	 * @param obj
	 * @return
	 */

	public static boolean isObjectNull(Object obj) {
		if (obj != null) {
			return false;
		}
		return true;

	}

	/**
	 * �?��是否为空
	 */
	public static boolean isNull(Object strObj) {
		String str = strObj + "";
		if (!"".equals(str) && !"null".equals(str) && !"undefined".equals(str)) {
			return false;
		}
		return true;
	}

	public static boolean isNullNaN(Object strObj) {
		String str = strObj + "";
		if (!"".equals(str) && !"null".equals(str)) {
			return false;
		}
		if ("NaN".equals(strObj)) {
			return false;
		}
		return true;
	}

	/**
	 * �?��邮箱
	 * 
	 * @param strObj
	 * @return
	 */
	public static boolean checkEmail(Object strObj) {
		String str = strObj + "";
		String match = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
		Pattern pattern = Pattern.compile(match);
		Matcher matcher = pattern.matcher(str);
		if (matcher.matches()) {
			return true;
		}
		return false;
	}
 
	public static boolean idlbbdNumber(String number) {
		String rgx = "^\\d{15}|^\\d{17}([0-9]|X|x)$";

		return isCorrect(rgx, number);
	}

	// 正则验证
	public static boolean isCorrect(String rgx, String res) {
		Pattern p = Pattern.compile(rgx);

		Matcher m = p.matcher(res);

		return m.matches();
	}

	public static boolean vIDNumByRegex(String idNum) {

		String curYear = "" + Calendar.getInstance().get(Calendar.YEAR);
		int y3 = Integer.valueOf(curYear.substring(2, 3));
		int y4 = Integer.valueOf(curYear.substring(3, 4));
		// 43 0103 1973 0 9 30 051 9
		return idNum
				.matches("^(1[1-5]|2[1-3]|3[1-7]|4[1-6]|5[0-4]|6[1-5]|71|8[1-2])\\d{4}(19\\d{2}|20([0-"
						+ (y3 - 1)
						+ "][0-9]|"
						+ y3
						+ "[0-"
						+ y4
						+ "]))(((0[1-9]|1[0-2])(0[1-9]|[1-2][0-9]|3[0-1])))\\d{3}([0-9]|x|X)$");
		// 44 1825 1988 0 7 1 3 003 4
	}

	private static int ID_LENGTH = 17;

	public static boolean vIDNumByCode(String idNum) {

		// 系数列表
		int[] ratioArr = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };

		// 校验码列表
		char[] checkCodeList = { '1', '0', 'X', '9', '8', '7', '6', '5', '4',
				'3', '2' };

		// 获取身份证号字符数组
		char[] cIds = idNum.toCharArray();

		// 获取最后一位（身份证校验码）
		char oCode = cIds[ID_LENGTH];
		int[] iIds = new int[ID_LENGTH];
		int idSum = 0;// 身份证号第1-17位与系数之积的和
		int residue = 0;// 余数(用加出来和除以11，看余数是多少？)

		for (int i = 0; i < ID_LENGTH; i++) {
			iIds[i] = cIds[i] - '0';
			idSum += iIds[i] * ratioArr[i];
		}

		residue = idSum % 11;// 取得余数

		return Character.toUpperCase(oCode) == checkCodeList[residue];
	}

	public static boolean vId(String idNum) {
		return vIDNumByCode(idNum) && vIDNumByRegex(idNum);
	}

	/**
	 * �?��是否为电话号�?
	 */
	public static boolean checkNumber(Object phoneNumber) {
		boolean isValid = false;
		String expression = "^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{5})$";
		String expression2 = "^\\(?(\\d{3})\\)?[- ]?(\\d{4})[- ]?(\\d{4})$";
		CharSequence inputStr = phoneNumber + "";
		Pattern pattern = Pattern.compile(expression);
		Matcher matcher = pattern.matcher(inputStr);
		Pattern pattern2 = Pattern.compile(expression2);
		Matcher matcher2 = pattern2.matcher(inputStr);
		if (matcher.matches() || matcher2.matches()) {
			isValid = true;
		}
		return isValid;
	}

	/**
	 * �?��str的长度是否达到要�?
	 * 
	 * @param str
	 * @param length
	 * @return
	 */
	public static boolean checkLength(Object strObj, int length) {
		String str = strObj + "";
		if (str.length() >= length) {
			return true;
		}
		return false;
	}

	/**
	 * �?��字符串的长度
	 * 
	 * @param strObj
	 * @param length
	 * @return
	 */
	public static boolean checkLengthEq(Object strObj, int length) {
		String str = strObj + "";
		if (str.length() == length) {
			return true;
		}
		return false;
	}

	public static boolean checkLength(Object strObj, int start, int end) {
		String str = strObj + "";
		if (str.length() >= start && str.length() <= end) {
			return true;
		}
		return false;
	}

	/**
	 * �?��请求返回是否正确
	 */
	public static boolean checkStatusOk(String status) {
		if ("1".equals(status)) {
			return true;
		}
		return false;
	}

	public static boolean checkStatusOk(int status) {
		if (20000 == status) {
			return true;
		}
		return false;
	}

	/**
	 * �?��string是否为o
	 * 
	 * @param value
	 * @return
	 */
	/*
	 * public static boolean checkZero(String value) { int valueInt =
	 * StringUtil.toInt(value); if (valueInt == 0) { return true; } return
	 * false; }
	 */
	/**
	 * 获得 包含中文的长度
	 */
	public static int getWordCount(String s) {
		int length = 0;
		for (int i = 0; i < s.length(); i++) {
			int ascii = Character.codePointAt(s, i);
			if (ascii >= 0 && ascii <= 255)
				length++;
			else
				length += 2;

		}
		return length;

	}

	public static boolean checkWorkCountLength(Object strObj, int start, int end) {
		String str = strObj + "";
		int len = getWordCount(str);
		if (len >= start && len <= end) {
			return true;
		}
		return false;
	}
	
	/**
	 * 
	* <p>Title: isNull</p>
	* <p>Description: 判断对象是否为空</p>
	* @param object
	* @return
	* @author King
	* @date 2016年12月20日
	 */
	public static boolean isNull(String object){
		if(null == object || "".equals(object) || "".equals(object.trim())){
			return true;
		}else{
			return false;
		}
	}

}
